package com.sxmd.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * Created by bigyu on 2016/9/1.
 */

public class FreeSchedule extends View {


    private int measureHeight;
    private int measureWidth;

    // 日期和星期
    private Map<String, String> mapDate;
    // 空闲时间
    private List<Prog> progs;
    private String month = "12月";

    private int dateColor = R.color.color_dateColor;  // 日期和星期的颜色
    private int timeColor = R.color.color_timeColor;   // 时间表的颜色
    private int currentDayColor = R.color.color_currentDayColor; // 当天的颜色
    private int hLineColor = R.color.color_hLineColor; // 横线的颜色
    private int vLineColor = R.color.color_vLineColor;// 空闲的颜色
    private int timeHeight;
    private int txtSize = 30;


    private String[] time = new String[]{"07:00", "08.00", "09:00", "10:00", "11:00", "12:00", "0", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "0", "19:00", "20:00", "21:00", "22:00"};


    public FreeSchedule(Context context) {
        super(context);
        initView();
    }

    Paint paint;
    Rect dayRect , weekRect, timeRect;

    /**
     * 初始化
     */
    private void initView() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);
        dayRect = new Rect();
        weekRect = new Rect();
        timeRect = new Rect();
    }

    public FreeSchedule(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public FreeSchedule(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        measureWidth = getMeasuredWidth();
        measureHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //日期每一个控件的宽度
        int singleWidth = measureWidth / (7 + 2);
        //竖直方向时间每一项的高度
        int singHeight = measureHeight / (time.length + 3);


        Iterator<Map.Entry<String, String>> it = mapDate.entrySet().iterator();
        int index = 0;
        paint.setTextSize(txtSize);
        paint.setStrokeWidth(1);

        //星期和月份
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            index++;
            if (index == 1) {
                paint.setColor(getResources().getColor(currentDayColor));
            } else {
                paint.setColor(getResources().getColor(dateColor));
            }
            //横向每一项的开始位置(x坐标)
            int hEveryX = singleWidth + singleWidth * index;
            //横向每一项的开始位置(y坐标)
            int vEveryY = 40;
            //偏移量
            int offSet = 0;

            //日期（  1 - 31 号）
            String day =entry.getKey();
            // 星期 （ 1- 7 ）
            String week = entry.getValue();

            //写日期
            canvas.drawText(day, singleWidth * (index + 1), singHeight / 2, paint);

            //写星期
            canvas.drawText(week, singleWidth * (index + 1) , singHeight+txtSize /2 , paint);
        }
        //设置月份
        canvas.drawText(month, singleWidth , 50, paint);
        paint.setStrokeWidth(1);

        //设置时间
        for (int i = 0; i < time.length; i++) {
            paint.setColor(getResources().getColor(timeColor));
//            int timeStartX = 40 + txtSize + 15;  // int vEveryY = 40;
            int timeStartX = singleWidth;
            int vHeight = singHeight+txtSize /2  + singHeight * (i + 1)  ;
            paint.getTextBounds(time[i], 0, time[i].length(), timeRect);
             timeHeight = timeRect .height() ;
            if (!time[i].equals("0")) {
//                                                      日期和星期的垂直  向下100偏移
                canvas.drawText(time[i], timeStartX , vHeight, paint);
                //划横线
                canvas.drawLine(singleWidth *2  - txtSize  , vHeight - timeHeight /2 , singleWidth + singleWidth * 7 + 14, vHeight -timeHeight /2, paint);
            } else {
                canvas.drawLine(timeStartX -txtSize, vHeight -timeHeight /2, singleWidth + singleWidth * 7 + 14, vHeight-timeHeight /2, paint);
            }
        }


        paint.setStyle(Paint.Style.FILL_AND_STROKE);

        //画进度
        DrawProgress(canvas, singleWidth, singHeight);

    }

    //画进度
    private void DrawProgress(Canvas canvas, int singleWidth, int singHeight) {

        for (int i = 0; i < progs.size(); i++) {

            paint.setStrokeWidth(txtSize);
            Prog prog = progs.get(i);
            if (prog.getDay() != 0) {
                paint.setColor(getResources().getColor(vLineColor));
            } else {
                paint.setColor(getResources().getColor(currentDayColor));
            }
            int placeX = singleWidth * 2 + singleWidth * prog.getDay() ;
            int placeStartY = prog.getStartTime();
            int placeEndY = prog.getEndTime();
            int vStartHeight =  singHeight  +timeHeight /2  + singHeight * (placeStartY + 1) + 8 ;
            int vEndHeight =singHeight+timeHeight /2  + singHeight * (placeEndY + 1) - timeHeight ;
            canvas.drawLine(placeX, vStartHeight, placeX, vEndHeight, paint);
            paint.setStrokeWidth(1);
            canvas.drawCircle(placeX, vStartHeight  , txtSize / 2 - 1, paint);
            canvas.drawCircle(placeX, vEndHeight , txtSize / 2 - 1, paint);
            if (prog.getDay() == 0) {
                paint.setColor(Color.WHITE);
                canvas.drawCircle(placeX, vStartHeight, txtSize / 4, paint);
                canvas.drawCircle(placeX, vEndHeight, txtSize / 4, paint);
            }
        }
    }


    //设置月份
    public void setMonth(String month) {
        this.month = month;
    }

    public Map<String, String> getMapDate() {
        return mapDate;
    }

    public void setMapDate(Map<String, String> mapDate) {
        this.mapDate = mapDate;
    }

    public List<Prog> getProgs() {
        return progs;
    }

    public void setProgs(List<Prog> progs) {
        this.progs = progs;
    }

    public String getMonth() {
        return month;
    }

    public int getDateColor() {
        return dateColor;
    }

    public void setDateColor(int dateColor) {
        this.dateColor = dateColor;
    }

    public int getTimeColor() {
        return timeColor;
    }

    public void setTimeColor(int timeColor) {
        this.timeColor = timeColor;
    }

    public int getCurrentDayColor() {
        return currentDayColor;
    }

    public void setCurrentDayColor(int currentDayColor) {
        this.currentDayColor = currentDayColor;
    }

    public int gethLineColor() {
        return hLineColor;
    }

    public void sethLineColor(int hLineColor) {
        this.hLineColor = hLineColor;
    }

    public int getvLineColor() {
        return vLineColor;
    }

    public void setvLineColor(int vLineColor) {
        this.vLineColor = vLineColor;
    }

    public int getTxtSize() {
        return txtSize;
    }

    public void setTxtSize(int txtSize) {
        this.txtSize = txtSize;
    }

    public String[] getTime() {
        return time;
    }

    public void setTime(String[] time) {
        this.time = time;
    }

}
